/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ItemEfficiencyPanel.java
 *
 * Created on Apr 8, 2010, 6:17:37 PM
 */

package edu.asu.cse360.client.ui.frame;

import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.client.model.ColumnModel;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.dto.ItemEfficiency;
import edu.asu.cse360.dto.ItemEfficiencyReport;
import edu.asu.cse360.editor.DateTimePropertyEditor;
import edu.asu.cse360.service.ItemReportService;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.AbstractTableModel;
import org.joda.time.DateTime;

/**
 *This class is responsible to display the item efficiency
 * @author ggreiner
 */
public class ItemEfficiencyPanel extends javax.swing.JPanel {
    private ComboBoxModel durationModel;
    private static final String DURATION_DAILY = "Daily";
    private static final String DURATION_WEEKLY = "Weekly";
    private static final String DURATION_MONTHLY = "Monthly";
    private static final String DURATION_ALL_TIME = "All Time";

    private ItemEfficiencyTableModel efficiencyTableModel;
    private DateTime startDate;
    private DateTime endDate;
    private ColumnModel<ItemEfficiency> tableColumns = new ColumnModel<ItemEfficiency>(){
        private String[] columnNames = new String[]{"Item Name", "Rating", "Frequency"};
        public Object getValue(ItemEfficiency target, int index) {
            Object value = null;
            switch(index){
                case 0:
                    value = target.getMenuItem().getName();
                    break;
                case 1:
                    value = target.getMenuItem().getRating();
                    break;
                case 2:
                    value = target.getFrequency();
                    break;
            }
            return value;
        }

        public String getColumnName(int index) {
            return columnNames[index];
        }

        public int getCount() {
            return columnNames.length;
        }
        
    };
    private ItemReportService reportSvc;
    private ItemEfficiencyReport efficiencyReport;

    /** Creates new form ItemEfficiencyPanel */
    public ItemEfficiencyPanel() {
        startDate = DateTimePropertyEditor.buildDate("04-05-2010@7:35:20");
        endDate = DateTimePropertyEditor.buildDate("04-05-2010@8:00:20");
        durationModel = new DefaultComboBoxModel(new String[]{DURATION_DAILY, DURATION_WEEKLY, DURATION_MONTHLY, DURATION_ALL_TIME});
        reportSvc = ServiceFactory.getItemReportService();
        efficiencyTableModel = new ItemEfficiencyTableModel(tableColumns);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        spinField = new com.toedter.components.JSpinField();
        durationBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        timeAgoLbl = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(efficiencyTableModel);
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1086, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
        );

        spinField.setMaximum(Integer.MAX_VALUE);
        spinField.setMinimum(0);
        spinField.setValue(1);
        spinField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                spinFieldPropertyChange(evt);
            }
        });

        durationBox.setModel(durationModel);
        durationBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                durationBoxActionPerformed(evt);
            }
        });

        jLabel1.setText("Time Period");

        timeAgoLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        timeAgoLbl.setText("Ago");

        jButton1.setText("Refresh");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(151, 151, 151)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(durationBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .add(62, 62, 62)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(spinField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 62, Short.MAX_VALUE)
                        .add(jButton1))
                    .add(timeAgoLbl))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel1)
                    .add(timeAgoLbl))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton1)
                    .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(spinField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(durationBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(200, 200, 200)
                        .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(273, 273, 273))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void spinFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_spinFieldPropertyChange
        handleTimeSelectionChange();
    }//GEN-LAST:event_spinFieldPropertyChange

    private void durationBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_durationBoxActionPerformed
        handleTimeSelectionChange();
    }//GEN-LAST:event_durationBoxActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        refreshTableModel();
    }//GEN-LAST:event_jButton1ActionPerformed

    private ComboBoxModel getDurationModel(){
        return durationModel;
    }

    private void refreshTableModel() {
        efficiencyReport = reportSvc.getEfficiencyReport(RestaurantContext.getRestaurant(), startDate, endDate);
        efficiencyTableModel.setItemEfficiencies(efficiencyReport.getItemEfficiencies());
    }

    private void handleTimeSelectionChange() {
        int timeAgo = spinField.getValue();
        endDate = new DateTime();
        Object selectedItem = durationBox.getSelectedItem();
        spinField.getSpinner().setEnabled(true);
        if(selectedItem.equals(DURATION_DAILY)){
            startDate = endDate.minusDays(timeAgo);
            timeAgoLbl.setText("Days Ago");
        }else if(selectedItem.equals(DURATION_MONTHLY)){
            startDate = endDate.minusMonths(timeAgo);
            timeAgoLbl.setText("Months Ago");
        }else if(selectedItem.equals(DURATION_WEEKLY)){
            startDate = endDate.minusWeeks(timeAgo);
            timeAgoLbl.setText("Weeks Ago");
        }else if(selectedItem.equals(DURATION_ALL_TIME)){
            startDate = new DateTime(0L);
            spinField.getSpinner().setEnabled(false);
            timeAgoLbl.setText("All Time");
        }
    }

    private class ItemEfficiencyTableModel extends AbstractTableModel{

        private ColumnModel columns;
        private List<ItemEfficiency> efficiencies;

        public ItemEfficiencyTableModel(ColumnModel columns){
            this.columns = columns;
            this.efficiencies = new ArrayList<ItemEfficiency>();
        }

        public int getRowCount() {
            return this.efficiencies.size();
        }

        public int getColumnCount() {
            return columns.getCount();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            ItemEfficiency target = efficiencies.get(rowIndex);
            return columns.getValue(target, columnIndex);
        }

        public void setItemEfficiencies(List<ItemEfficiency> efficiencies){
            this.efficiencies.clear();
            if(efficiencies != null){
                this.efficiencies.addAll(efficiencies);
            }
            super.fireTableDataChanged();
        }

        @Override
        public String getColumnName(int column) {
            return columns.getColumnName(column);
        }
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox durationBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private com.toedter.components.JSpinField spinField;
    private javax.swing.JLabel timeAgoLbl;
    // End of variables declaration//GEN-END:variables

}
